class MapControl {

    public constructor() {
    }

    /**
     * createElementAllMap
     */
    public createElementAllMap() {
        this.createAllMap();
    }

    public createElements(num: number): string[] {
        let tyopes: string[] = [];
        for (let i = 0; i < num; i++) {
            tyopes.push(this.createType());
        }
        return tyopes;
    }

    public changeTypeByID(id: number) {
        GameData.elements[id].type = this.createType();
    }

    public updateMapLocation() {
        let ids: number[] = [];
        for (let i = 0; i < LinkLogic.lines.length; i++) {
            for (let j = 0; j < LinkLogic.lines[i].length; j++) {
                let result: boolean = false;
                for (let p = 0; p < ids.length; p++) {
                    if (ids[p] == LinkLogic.lines[i][j]) {
                        result = true;
                    }
                }
                if (!result) {
                    this.changeTypeByID(LinkLogic.lines[i][j]);
                    ids.push(LinkLogic.lines[i][j]);
                }
            }
        }

        let colarr: number[] = [];
        for (let i = 0; i < ids.length; i++) {
            let result: boolean = false;
            for (let j = 0; j < colarr.length; j++) {
                if (colarr[j] == GameData.elements[ids[i]].location % GameData.MaxColumn) {
                    result = true;
                }
                if (!result) {
                    colarr.push(GameData.elements[ids[i]].location % GameData.MaxColumn);
                }
            }
        }

        let colelids: number[];
        for (let i = 0; i < colarr.length; i++) {

            let newcolids: number[] = [];
            let removeids: number[] = [];
            let result: boolean = false;
            for (let j = GameData.MaxRow; j >= 0; j--) {
                for (let p = 0; p < ids.length; p++) {
                    removeids.push(ids[p]);
                    result = true;
                }
                if (!result) {
                    if (GameData.mapData[j][colarr[i]] != -1) {
                        newcolids.push(GameData.mapData[j][colarr[i]]);
                    }
                }
            }

            newcolids = newcolids.concat(removeids);
            for (let j = GameData.MaxRow - 1; j >= 0; j--) {
                if (GameData.mapData[j][colarr[i]] != -1) {
                    GameData.mapData[j][colarr[i]] = newcolids[0];
                    GameData.elements[newcolids[0]].location - j * GameData.MaxRow + colarr[i];
                    newcolids.shift();
                }
            }
        }
    }

    private createAllMap() {
        let len: number = GameData.MaxRow * GameData.MaxColumn;
        let type: string = "";
        let havelink: boolean = true;
        let id: number = 0;
        let ztype: string = "";
        let htype: string = "";
        for (let i = 0; i < GameData.MaxRow; i++) {
            for (let j = 0; j < GameData.MaxColumn; j++) {
                while (havelink) {
                    type = this.createType();
                    if (i > 1 && GameData.mapData[i - 1][j] != -1 && GameData.mapData[i - 2][j] != -1) {
                        if (GameData.elements[GameData.mapData[i - 1][j]].type == GameData.elements[GameData.mapData[i - 2][j]].type) {
                            ztype = GameData.elements[GameData.mapData[i - 1][j]].type;
                        }
                    }
                    if (j > 1 && GameData.mapData[i][j - 1] != -1 && GameData.mapData[i][j - 2] != -1) {
                        if (GameData.elements[GameData.mapData[i][j - 1]].type == GameData.elements[GameData.mapData[i][j - 2]].type) {
                            htype = GameData.elements[GameData.mapData[i][j - 1]].type;
                        }
                    }
                    if (type != ztype && type != htype) {
                        havelink = false;
                    }
                }
                id = GameData.unusedElements[0];
                GameData.elements[id].type = type;
                GameData.elements[id].location = i * GameData.MaxRow + j;
                GameData.mapData[i][j] = id;
                GameData.unusedElements.shift();
                havelink = true;
                ztype = "";
                htype = "";
            }
        }
    }

    private createType(): string {
        return GameData.elementTypes[Math.floor(Math.random() * GameData.elementTypes.length)].toString();
    }
}